// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Provides a presenter class for displaying and operating the post browse/
 * search UI.
 */

goog.provide('rb.browse.Browser');

goog.require('goog.Disposable');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events.EventHandler');
goog.require('goog.string');

goog.require('rb.browse.templates');



/**
 * A presenter class that sets up and operates the post browsing and searching
 * UI.
 *
 * @param {rb.ServiceProvider} sp The app's service provider.
 *
 * @constructor
 * @extends {goog.Disposable}
 */
rb.browse.Browser = function(sp) {
  /**
   * @type {rb.ServiceProvider}
   * @private
   */
  this.sp_ = sp;

  /**
   * @type {goog.events.EventHandler}
   * @private
   */
  this.handler_ = new goog.events.EventHandler(this);
};
goog.inherits(rb.browse.Browser, goog.Disposable);


/**
 * Initial set-up method.  Send a request to the server for the first batch of
 * post details.
 */
rb.browse.Browser.prototype.setUp = function() {
  var onSuccess = goog.bind(this.gotPostList_, this),
      onFailure = goog.bind(this.dontGotPostList_, this),
      cmd = new rb.post.GetPostsCommand(onSuccess, onFailure, 20, false),
      rpc = this.sp_.getRPCService();

  rpc.execute(cmd);
};


/**
 * onSuccess callback for the request to retrieve the list of posts.
 *
 * @param {Array.<rb.post.BlogPost>} posts The array of blog post objects.
 * @private
 */
rb.browse.Browser.prototype.gotPostList_ = function(posts) {
  var content = this.sp_.getContentRoot(),
      postData = [],
      templateData = {
        posts: postData
      };

  for (var i = 0, len = posts.length; i < len; ++i) {
    var post = /** @type {rb.post.BlogPost} */ (posts[i]),
        author = post.getAuthor();

    postData[i] = {
      title: post.getTitle(),
      authorName: author.displayName,
      authorTitle: author.title,
      created: post.getCreatedForDisplay(),
      slug: post.getSlug()
    };
  }
  content.innerHTML = rb.browse.templates.browseUI(templateData);
  var handler = this.handler_,
      table = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.TABLE,
                                                    undefined, content)[0];

  handler.listen(table, goog.events.EventType.CLICK, this.postClicked_);
};


/**
 * onFailure callback for the request to retrieve the list of posts.
 *
 * @private
 */
rb.browse.Browser.prototype.dontGotPostList_ = function() {
};


/**
 * Click handler for blog post list entries.
 *
 * @param {goog.events.BrowserEvent} e The click event.
 * @private
 */
rb.browse.Browser.prototype.postClicked_ = function(e) {
  var target = e.target,
      row = goog.dom.getAncestorByTagNameAndClass(target,
                                                  goog.dom.TagName.TR),
      id = row.id,
      prefix = 'browse-list-entry-',
      slug = goog.string.removeAt(id, 0, prefix.length);

  if (slug !== '') {
    relief.utils.setLocationHash('/post/' + slug);
  }
};


/**
 * @inheritDoc
 */
rb.browse.Browser.prototype.disposeInternal = function() {
  this.handler_.dispose();
  this.handler_ = null;
};
